package com.lzk.system.controller;

import com.lzk.basic.util.JsonResult;
import com.lzk.basic.util.PageList;
import com.lzk.system.domain.Dictionarytype;
import com.lzk.system.query.DictionarytypeQuery;
import com.lzk.system.service.IDictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionarytype")

@Api(value = "产品的API",description="产品类型的CRUD功能")
public class DictionarytypeController {


    @Autowired
    private IDictionarytypeService dictionarytypeService;

    /**
     * 查询所有数据
     *
     * @return
     */
    @ApiOperation(value = "查询所有数据" )
    @GetMapping
    public List<Dictionarytype> findAll(){
        return dictionarytypeService.loadAll();
    }

    /**
     * 查询一个 接收参数id
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询一个数据" )
    @GetMapping("/{id}")
    public Dictionarytype findOne(@PathVariable("id") Long id){
        return dictionarytypeService.loadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一个数据" )
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            dictionarytypeService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }

    /**
     * 添加或修改
     * @param dictionarytype
     * @return
     */
    @ApiOperation(value = "添加或修改数据" )
    @PutMapping
    public JsonResult updateOrAdd(@RequestBody Dictionarytype dictionarytype){
        try {
            if (dictionarytype.getId() == null){
                dictionarytypeService.save(dictionarytype);
            }else {
                dictionarytypeService.update(dictionarytype);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
    /**
     * 分页查询或高级查询
     * @param dictionarytypeQuery
     * @return
     */
    @ApiOperation(value = "分页查询或高级查询" )
    @PostMapping
    public PageList<Dictionarytype> queryPage(@RequestBody DictionarytypeQuery dictionarytypeQuery){
        return dictionarytypeService.queryData(dictionarytypeQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除一个数据" )
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            dictionarytypeService.patchRemove(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
}
